package com.ruoyi.rsikManage.service.Impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.rsikManage.domain.MonitorPoint;
import com.ruoyi.rsikManage.domain.VideoDiagnosisResult;
import com.ruoyi.rsikManage.domain.vo.MonitorPointDiagnosisResultVo;
import com.ruoyi.rsikManage.mapper.VideoDiagnosisMapper;
import com.ruoyi.rsikManage.service.JyService;
import com.ruoyi.rsikManage.service.VideoDiagnosisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class VideoDiagnosisImpl implements VideoDiagnosisService {
    @Autowired
    VideoDiagnosisMapper videoDiagnosisResultMapper;
    @Autowired
    JyService jyService;

    public void updateAllVideoDiagnosisResult() {
        System.out.println("==============添加历史视频质量诊断结果：开始=====================");
        // 1 查各监控点及其mysql中最后的诊断时间
        List<MonitorPoint> monitorPointList = videoDiagnosisResultMapper.selectLastDiagnosisTimeInLocal();
        for (MonitorPoint monitor : monitorPointList) {
            // 2.查此监控点最新诊断结果集
            Date lastDiagnosisTimeInLocal = new Date(0);
            if (monitor.getLastDiagnosisTime() != null) {
                lastDiagnosisTimeInLocal = monitor.getLastDiagnosisTime();
            }
            //时间加一秒
            Date lastDiagnosisTime = new Date(lastDiagnosisTimeInLocal.getTime() + 1000L);
            String lastDiagnosisTimeStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(lastDiagnosisTime);
            //System.out.println("==========================" + "监控点"+ monitor.getName() + "的mysql最新诊断时间为" + lastDiagnosisTimeStr);
            lastDiagnosisTimeStr = "2023-10-26 00:00:00";

            String diagnosisResultListJsonStr = jyService.getVideoDiagnosisResults(monitor.getName(), lastDiagnosisTimeStr,1, Integer.MAX_VALUE);
            List<VideoDiagnosisResult> diagnosisResultList = toDiagnosisResultListObject(diagnosisResultListJsonStr);

            // 3.将结果集加入Mysql，若集中含非正常结果，报警且统计。
//            for (VideoDiagnosisResult diagnosisResult : diagnosisResultList) {
//                //System.out.println(diagnosisResult.toString());
//                //System.out.println("------------------" + diagnosisResult.getChannelName() + "," + diagnosisResult.getDiagnosisTime());
//                //视频质量监测结果是很差或者失败就报警
//                if (diagnosisResult.getResult() == 3 || diagnosisResult.getResult() == 4) {
//                    //发送监控点视频质量诊断异常的报警信息
//                    RestTemplate restTemplate = new RestTemplate();
//                    HttpEntity httpEntity = new HttpEntity(diagnosisResult);
//                    String url = "http://localhost:"+ serverPort+ "/zdzh/alarms/diagnosis-abnormal-alarm";
//                    ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
//                    //加入统计表
//                    videoDiagnosisResultMapper.recordAbnormalDiagnosticResult(diagnosisResult.getChannelCode(), diagnosisResult.getDiagnosisTime(), diagnosisResult.getResult());
//                }
//                videoDiagnosisResultMapper.insertVideoDiagnosisResult(diagnosisResult);
//            }
            videoDiagnosisResultMapper.insertVideoDiagnosisResultList(diagnosisResultList);

        }
        System.out.println("==============添加历史视频质量诊断结果：结束=====================");
    }
    List<VideoDiagnosisResult> toDiagnosisResultListObject(String diagnosisResultListJsonStr) {
        List<VideoDiagnosisResult> diagnosisResultList = new ArrayList<>();
        JSONArray diagnosisResultJsonArray = JSONObject.parseObject(diagnosisResultListJsonStr).getJSONObject("data").getJSONArray("data");
        for (int i = 0; i < diagnosisResultJsonArray.size(); i++) {
            JSONObject j = diagnosisResultJsonArray.getJSONObject(i);
            //诊断结果对象
            VideoDiagnosisResult diagnosisResult = new VideoDiagnosisResult();
            //初始化诊断结果对象
            diagnosisResult.setChannelCode(j.getString("ape_id"));
            diagnosisResult.setChannelName(j.getString("channel_name"));
            diagnosisResult.setDeviceIp(j.getString("dev_ip"));
            diagnosisResult.setDeviceName(j.getString("dev_name"));
            diagnosisResult.setIsOnline(j.getString("is_online"));
            diagnosisResult.setStreamFormat(j.getString("stream_format"));
            diagnosisResult.setCacType(j.getInteger("cac_type"));
            diagnosisResult.setOrgName(j.getString("org_name"));
            diagnosisResult.setDiagnosisTime(j.getDate("diagnosis_time"));
            diagnosisResult.setResult(j.getInteger("result"));
            diagnosisResult.setDiagnosisResultReason(j.getString("diagnosis_result_reason"));
            diagnosisResult.setColorCast(j.getInteger("color_cast"));
            diagnosisResult.setContrastRatio(j.getInteger("contrast_ratio"));
            diagnosisResult.setCrossStriation(j.getInteger("cross_striation"));
            diagnosisResult.setDefinition(j.getInteger("definition"));
            diagnosisResult.setFreeze(j.getInteger("freeze"));
            diagnosisResult.setHighLuminance(j.getInteger("high_luminance"));
            diagnosisResult.setJitter(j.getInteger("jitter"));
            diagnosisResult.setLoss(j.getInteger("loss"));
            diagnosisResult.setLowLuminance(j.getInteger("low_luminance"));
            diagnosisResult.setNoise(j.getInteger("noise"));
            diagnosisResult.setOcclusion(j.getInteger("occlusion"));
            diagnosisResult.setRollingStripe(j.getInteger("rolling_stripe"));
            diagnosisResult.setSceneChange(j.getInteger("scene_change"));
            diagnosisResult.setStripe(j.getInteger("stripe"));
            diagnosisResult.setTransverseWave(j.getInteger("transverse_wave"));
            //加入结果集
            diagnosisResultList.add(diagnosisResult);
        }
        return diagnosisResultList;
    }
    @Override
    public List<VideoDiagnosisResult> selectVideoDiagnosisResult() {
        return videoDiagnosisResultMapper.selectVideoDiagnosisResult();
    }

    @Override
    public int insertVideoDiagnosisResult(VideoDiagnosisResult videoDiagnosisResult) {
        return videoDiagnosisResultMapper.insertVideoDiagnosisResult(videoDiagnosisResult);
    }

    @Override
    public int updateVideoDiagnosisResultById(VideoDiagnosisResult videoDiagnosisResult) {
        return videoDiagnosisResultMapper.updateVideoDiagnosisResultById(videoDiagnosisResult);
    }

    @Override
    public int deleteVideoDiagnosisResultById(Integer id) {
        return videoDiagnosisResultMapper.deleteVideoDiagnosisResultById(id);
    }

    @Override
    public int deleteVideoDiagnosisResultByIds(Integer[] ids) {
        return videoDiagnosisResultMapper.deleteVideoDiagnosisResultByIds(ids);
    }

    @Override
    public int deleteAllVideoDiagnosisResult() {
        return videoDiagnosisResultMapper.deleteAllVideoDiagnosisResult();
    }

    @Override
    public List<MonitorPointDiagnosisResultVo> selectHistoryDiagnosisResult(HashMap<String, Object> param) {
        return videoDiagnosisResultMapper.selectHistoryDiagnosisResult(param);
    }

    @Override
    public void recordAbnormalDiagnosticResultList(List<VideoDiagnosisResult> abnormalVideoDiagnosisResultList) {
        videoDiagnosisResultMapper.recordAbnormalDiagnosticResultList(abnormalVideoDiagnosisResultList);
    }

    @Override
    public void recordAbnormalDiagnosticResultListForUnitCodes(List<VideoDiagnosisResult> abnormalVideoDiagnosisResultList) {
        videoDiagnosisResultMapper.recordAbnormalDiagnosticResultListForUnitCodes(abnormalVideoDiagnosisResultList);
    }

    @Override
    public void insertVideoDiagnosisResultList(List<VideoDiagnosisResult> list) {
        videoDiagnosisResultMapper.insertVideoDiagnosisResultList(list);

    }

    @Override
    public void insertVideoDiagnosisResultListForUnitCodes(List<VideoDiagnosisResult> list) {
        videoDiagnosisResultMapper.insertVideoDiagnosisResultListForUnitCodes(list);

    }

    @Override
    public void deleteVideoDiagnosisResultHistory() {
        videoDiagnosisResultMapper.deleteVideoDiagnosisResultHistory();
    }
}
